package contents.entities;

import java.util.Comparator;
import java.text.ParseException;

public class Message {
    public static class ByDateComparator implements Comparator {
        public int compare(Object object1, Object object2) {
            Message message1 = (Message) object1;
            Message message2 = (Message) object2;
            
            java.text.SimpleDateFormat datetimeFormat = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            try {
                java.util.Date date1 = datetimeFormat.parse(message1.getDate() + " " + message1.getTime());
                java.util.Date date2 = datetimeFormat.parse(message2.getDate() + " " + message2.getTime());
                // return negative of result, newer dates go first
                return -date1.compareTo(date2);
            } catch (ParseException exception) {
                // this should never happen
                return 0;
            }
        }   
    }
    
    public static class ByPeerAlphabeticallyComparator implements Comparator {
        public int compare(Object object1, Object object2) {
            Message message1 = (Message) object1;
            Message message2 = (Message) object2;           
            
            String lastName1 = message1.getRemotePeerLastName();
            String lastName2 = message2.getRemotePeerLastName();
            if (lastName1.equals(lastName2)) {
                String firstName1 = message1.getRemotePeerFirstName();
                String firstName2 = message2.getRemotePeerFirstName();
                if (firstName1.equals(firstName2)) {
                    String role1 = message1.getRemotePeerRole();
                    String role2 = message2.getRemotePeerRole();
                    if (role1.equals(role2)) {
                        Integer id1 = message1.getRemotePeerId();
                        Integer id2 = message2.getRemotePeerId();
                        if (id1.equals(id2)) {
                            
                            java.text.SimpleDateFormat datetimeFormat = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
                            try {
                                java.util.Date date1 = datetimeFormat.parse(message1.getDate() + " " + message1.getTime());
                                java.util.Date date2 = datetimeFormat.parse(message2.getDate() + " " + message2.getTime());
                                // return negative of result, newer dates go first
                                return -date1.compareTo(date2);
                            } catch (ParseException exception) {
                                // this should never happen
                                return 0;
                            }
                            
                        } else {
                            return id1.compareTo(id2);
                        }
                    } else {
                        return role1.compareTo(role2);
                    }
                } else {
                    return firstName1.compareTo(firstName2);
                }
            } else {
                return lastName1.compareTo(lastName2);
            }
        }   
    }
    
    private int messageId;
    private String messageOrigin;
        
    private int remotePeerId;
    private String remotePeerRole;
    private String remotePeerFirstName;
    private String remotePeerLastName;
        
    private String text;
    private String date;
    private String time;
    
    public Message(int messageId, String messageOrigin, int remotePeerId, String remotePeerRole, String remotePeerFirstName,
                   String remotePeerLastName, String text, String date, String time) {
        this.messageId = messageId;
        this.messageOrigin = messageOrigin;
        this.remotePeerId = remotePeerId;
        this.remotePeerRole = remotePeerRole;
        this.remotePeerFirstName = remotePeerFirstName;
        this.remotePeerLastName = remotePeerLastName;
        this.text = text;
        this.date = date;
        this.time = time;
    }

    public int getMessageId() {
        return messageId;
    }

    public String getMessageOrigin() {
        return messageOrigin;
    }

    public int getRemotePeerId() {
        return remotePeerId;
    }

    public String getRemotePeerFirstName() {
        return remotePeerFirstName;
    }
    
    public String getRemotePeerLastName() {
        return remotePeerLastName;
    }
    
    public String getRemotePeerFullName() {
        if (remotePeerRole.equals("admin")) {
            return remotePeerLastName;
        } else {
            return remotePeerFirstName + " " + remotePeerLastName;
        }
    }

    public String getRemotePeerRole() {
        return remotePeerRole;
    }

    public String getText() {
        return text;
    }

    public String getDate() {
        return date;
    }
    
    public String getTime() {
        return time;
    }
}
